package com.zhiwang.xiaoqu.widget

import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import com.zhiwang.xiaoqu.R
import kotlinx.android.synthetic.main.icon_text_view_layout.view.*
import kotlinx.android.synthetic.main.simple_dialog_layout.view.*
import java.lang.IllegalStateException

class SimpleDialog private constructor(private var dialog: AlertDialog) {


    /**
     *
     *
     *       title
     * -----------------------
     *       image
     *
     *       summary
     *        desc
     *   negative  positive (single)
     *
     *
     *
     */

    class Builder {

        private var onShowListener: DialogInterface.OnShowListener? = null
        private var dismissListener: OnDismissListener? = null
        private var title: String = ""
        private var titleColorRes: Int = 0
        private var titleTextSizeSp: Float = 18f

        private var posterRes: Int = 0

        private var summaryText: String = ""
        private var summaryTextColorRes: Int = 0
        private var summaryTextSizeSp: Float = 14f

        private var descText: String = ""
        private var descTextColorRes: Int = 0
        private var descTextSizeSp: Float = 12f

        private var shouldSingleButton: Boolean = false


        private var singleButtonBackgroundRes = R.drawable.btn_w230
        private var singleButtonText: String = ""
        private var singleButtonTextColorRes: Int = 0
        private var singleButtonTextSizeSp: Float = 14f

        private var negativeText: String = ""
        private var negativeBackgroundRes: Int = 0
        private var negativeTextColorRes: Int = 0
        private var negativeTextSizeSp = 13f
        private var negativeButtonClickListener: View.OnClickListener? = null

        private var positiveText: String = ""
        private var positiveBackgroundRes: Int = 0
        private var positiveTextColorRes: Int = 0
        private var positiveTextSizeSp = 13f
        private var positiveButtonClickListener: View.OnClickListener? = null

        private var cancellable: Boolean = false

        lateinit var context: Context

        constructor(context: Context) {
            this.context = context
        }


        /**
         * 顶部标题
         */
        fun title(title: String): Builder {
            this.title = title
            return this
        }

        fun titleColorRes(@ColorRes titleColorRes: Int): Builder {
            this.titleColorRes = titleColorRes
            return this
        }

        fun titleTextSizeSp(titleTextSizeSp: Float): Builder {
            this.titleTextSizeSp = titleTextSizeSp
            return this
        }

        /**
         * 图片,带图片的 弹框
         */
        fun poster(@DrawableRes posterRes: Int): Builder {
            this.posterRes = posterRes
            return this
        }

        /**
         * 中间的 小标题
         */
        fun summaryText(summaryText: String): Builder {
            this.summaryText = summaryText
            return this
        }

        fun summaryTextColor(@ColorRes summaryTextColorRes: Int): Builder {
            this.summaryTextColorRes = summaryTextColorRes
            return this
        }

        fun summaryTextSizeSp(summaryTextSizeSp: Float): Builder {
            this.summaryTextSizeSp = summaryTextSizeSp
            return this
        }

        /**
         * 文字描述
         */
        fun descText(descText: String): Builder {
            this.descText = descText
            return this
        }

        fun descTextColor(descTextColorRes: Int): Builder {
            this.descTextColorRes = descTextColorRes
            return this
        }

        fun descTextSizeSp(descTextSizeSp: Float): Builder {
            this.descTextSizeSp = descTextSizeSp
            return this
        }


        fun singleButton(single: Boolean): Builder {
            this.shouldSingleButton = single
            return this
        }

        fun singleButton(text: String, textSizeSp: Float, singleButtonBackgroundRes: Int, textColorRes: Int): Builder {
            this.singleButtonText = text
            this.singleButtonTextSizeSp = textSizeSp
            this.singleButtonBackgroundRes = singleButtonBackgroundRes
            this.singleButtonTextColorRes = textColorRes
            return this
        }

        fun negativeButton(
            text: String,
            textSizeSp: Float,
            negativeBackgroundRes: Int,
            textColorRes: Int
        ): Builder {
            this.negativeText = text
            this.negativeBackgroundRes = negativeBackgroundRes
            this.negativeTextSizeSp = textSizeSp
            this.negativeTextColorRes = textColorRes
            return this
        }

        fun onNegativeButtonClick(func: ((v: View) -> Unit)): Builder {
            this.negativeButtonClickListener = View.OnClickListener { v -> func(v) }
            return this
        }

        fun positiveButton(text: String, textSizeSp: Float, positiveBackgroundRes: Int, textColorRes: Int): Builder {
            this.positiveText = text
            this.positiveBackgroundRes = positiveBackgroundRes
            this.positiveTextSizeSp = textSizeSp
            this.positiveTextColorRes = textColorRes
            return this
        }

        fun onPositiveButtonClick(func: ((v: View) -> Unit)): Builder {
            this.positiveButtonClickListener = View.OnClickListener { v -> func(v) }
            return this
        }

        fun cancellable(cancellable: Boolean): Builder {
            this.cancellable = cancellable
            return this
        }

        fun setOnDismissListener(dismissListener: OnDismissListener): Builder {
            this.dismissListener = dismissListener
            return this
        }

        fun setOnShowListener(onShowListener: DialogInterface.OnShowListener): Builder {
            this.onShowListener = onShowListener
            return this
        }


        lateinit var dialogView: View

        fun create(): SimpleDialog {

            dialogView = LayoutInflater.from(context)
                .inflate(R.layout.simple_dialog_layout, null)

            processDialogItem(dialogView.dialog_title, title, titleTextSizeSp, titleColorRes, 0, 0, null)

            if (title.isEmpty()) dialogView.dialog_title_divider.visibility = View.GONE

            processDialogItem(dialogView.poster, "", 0f, 0, 0, posterRes, null)

            //summary text
            processDialogItem(
                dialogView.poster,
                summaryText,
                summaryTextSizeSp,
                summaryTextColorRes,
                0,
                posterRes,
                null
            )

            //desc
            processDialogItem(dialogView.desc, descText, descTextSizeSp, descTextColorRes, 0, 0, null)

            //single button
            if (shouldSingleButton) {
                dialogView.ll_button_group.visibility = View.GONE
                processDialogItem(
                    dialogView.singleButton,
                    singleButtonText,
                    singleButtonTextSizeSp,
                    singleButtonTextColorRes,
                    singleButtonBackgroundRes,
                    0,
                    null
                )

            } else {
                dialogView.singleButton.visibility = View.GONE
                processDialogItem(
                    dialogView.negative_button,
                    negativeText,
                    negativeTextSizeSp,
                    negativeTextColorRes,
                    negativeBackgroundRes,
                    0,
                    negativeButtonClickListener
                )

                processDialogItem(
                    dialogView.positive_button,
                    positiveText,
                    positiveTextSizeSp,
                    positiveTextColorRes,
                    positiveBackgroundRes,
                    0,
                    positiveButtonClickListener
                )

            }

            val dialog = AlertDialog.Builder(context)
                .setView(dialogView)
                .setCancelable(cancellable)
                .setOnDismissListener {
                    dismissListener?.onDismiss(it)
                }
                .create()

            return SimpleDialog(dialog)

        }


        private fun processDialogItem(
            view: View,
            text: String,
            textSizeSp: Float,
            textColorRes: Int,
            backgroundRes: Int,
            imageRes: Int,
            listener: View.OnClickListener?
        ) {
            if (view is TextView) {
                if (text.isNotEmpty()) {
                    view.text = text
                    if (textColorRes != 0)
                        view.setTextColor(ContextCompat.getColor(context, textColorRes))
                    view.textSize = textSizeSp
                    if (backgroundRes != 0)
                        view.setBackgroundResource(backgroundRes)
                    listener?.apply { view.setOnClickListener(this) }
                } else view.visibility = View.GONE
            } else if (view is ImageView) {
                if (imageRes != 0)
                    view.setImageResource(imageRes)
                else view.visibility = View.GONE
            } else {
                throw IllegalStateException("不支持 TextView ImageView 以外的元素")
            }
        }

    }


    interface OnDismissListener {

        fun onDismiss(dialog: DialogInterface)
    }

    fun show() = dialog.show()

    fun singleButton(): TextView? = dialog.findViewById<TextView>(R.id.singleButton)

    fun dismiss() = dialog.dismiss()

}